package com.baili.dao.operater;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.baili.dao.base.BaseDao;
import com.baili.dto.Call;
import com.baili.dto.Check;
import com.baili.dto.ProductOrder;

/**
 * @author iceX
 * @version 创建时间：2016年3月31日 下午3:03:29
 * @Name: OperateDaoImpl.java
 */
@Repository("OperateDao")
public class OperateDaoImpl extends BaseDao implements OperateDao {

	@Override
	public List getCallLog(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList("operater.getCallLog",
				productOrder);
	}

	@Override
	public List getCallLogById(int id) {
		
		Map map = new HashMap();
		map.put("id", id);
		return this.sqlSessionTemplate
				.selectList("operater.getCallLogById", map);
	}

	@Override
	public List getEventType() {
		
		return this.sqlSessionTemplate.selectList("operater.getEventType");
	}

	@Override
	public void insertEventType(Call call) {

		this.sqlSessionTemplate.insert("operater.insertEventType", call);
	}

	@Override
	public void updateState(int id, int state) {
		Map map = new HashMap();
		map.put("state", state);
		map.put("id", id);
		this.sqlSessionTemplate.update("operater.updateState", map);
	}
	@Override
	public void updateOperationState(ProductOrder productOrder) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(productOrder.getState() == 2){
			
			ProductOrder order = this.sqlSessionTemplate.selectOne("productorder.getProductionOrderByRoundAndProductCode", productOrder);
			
			productOrder.setRealStartTime(sdf.format(new Date()));
			
			if((order.getKzType() == 1 && order.getState() == 5) || (order.getKzType() == 2 && (order.getState() == 4 || order.getState() == 6)) ){
				
				productOrder.setKzType(2);
				this.sqlSessionTemplate.update(
						"operater.updateOperationStateStartKZType", productOrder);
				
			}else if(order.getKzType() == 1 && (order.getState() == 1 || order.getState() == 4 || order.getState() == 6)){
				
				productOrder.setKzType(1);
				this.sqlSessionTemplate.update(
						"operater.updateOperationStateStartKZType", productOrder);
			}
			
			this.sqlSessionTemplate.update(
					"operater.updateOperationStateStart", productOrder);
			
		}else if(productOrder.getState() == 3){
			
			productOrder.setRealOverTime(sdf.format(new Date()));
			
			this.sqlSessionTemplate.update("operater.updateOperationStateOver", productOrder);
			
		}else if(productOrder.getState() == 1){
			
			productOrder.setState(1);
			
			productOrder.setRealOverTime("1970-01-01 08:00:00");
			
			productOrder.setRealStartTime("1970-01-01 08:00:00");
			
			this.sqlSessionTemplate.update("operater.updateOperationStateStartAndOver", productOrder);
		}
	}

	@Override
	public List<Map<String, Object>> getWarning(ProductOrder productOrder) {
		
		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
		
		if(productOrder.getKzType() != 0){
			
			lst = this.sqlSessionTemplate.selectList("operater.getWarning" ,productOrder);
		}

		return lst;
	}

	@Override
	public List getFinishWorkYOrN(ProductOrder productOrder) {

		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
		
		if(productOrder.getRemarks().equals("中底") || productOrder.getRemarks().equals("成型底") || productOrder.getRemarks().equals("前段")){
			
			if (productOrder.getFlag().equals("<>")) {
				
				lst = this.sqlSessionTemplate.selectList("operater.getFinishWorkNOperator",
						productOrder);
//				List<Map<String, Object>>lstScrap = this.sqlSessionTemplate.selectList("operater.getFinishWorkNScrap",
//						productOrder);
//				
//				if(lstScrap != null && lstScrap.size() > 0){
//					
//					lst.addAll(lstScrap);
//				}

			} else if (productOrder.getFlag().equals("=")) {

				lst = this.sqlSessionTemplate.selectList("operater.getFinishWorkYOperator",
						productOrder);
			}
		}else{
			
			if (productOrder.getFlag().equals("<>")) {
				
				if(productOrder.getRemarks().equals("开料") ){
					
					productOrder.setProcessName("帮面");
					
					productOrder.setKzType(1);
					
					lst = this.sqlSessionTemplate.selectList("operater.getFinishWorkNKType",
							productOrder);
				}else if(productOrder.getRemarks().equals("制帮") ){
					
					productOrder.setProcessName("帮面");
					
					productOrder.setKzType(2);
					lst = this.sqlSessionTemplate.selectList("operater.getFinishWorkNZType",
							productOrder);
				}
				
			} else if (productOrder.getFlag().equals("=")) {
				
				if(productOrder.getProcessName().equals("开料") ){
					
					lst = this.sqlSessionTemplate.selectList("operater.getFinishWorkYKType",
							productOrder);
				}else if(productOrder.getProcessName().equals("制帮") ){
					
					lst = this.sqlSessionTemplate.selectList("operater.getFinishWorkYZType",
							productOrder);
					
				}
			}
		}


		return lst;
	}

	@Override
	public List getRoundDetails(ProductOrder productOrder) {

		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

		if (productOrder.getFlag().equals("<>")) {
			
			if(productOrder.getProcessName().equals("开料") ){
				
				productOrder.setProcessName("帮面");
				
				lst = this.sqlSessionTemplate.selectList("operater.getRoundDetailsNKType",
						productOrder);
			}else if(productOrder.getProcessName().equals("制帮") ){
				
				productOrder.setProcessName("帮面");
				
				lst = this.sqlSessionTemplate.selectList("operater.getRoundDetailsNZType",
						productOrder);
			}else{
				
				lst = this.sqlSessionTemplate.selectList(
						"operater.getRoundDetailsN", productOrder);
				
			}


		} else if (productOrder.getFlag().equals("=")) {
			
			if(productOrder.getProcessName().equals("开料") ){
				
				lst = this.sqlSessionTemplate.selectList(
						"operater.getRoundDetailsYKType", productOrder);
			}else if(productOrder.getProcessName().equals("制帮") ){
				
				
				lst = this.sqlSessionTemplate.selectList(
						"operater.getRoundDetailsYZType", productOrder);
				
			}else{
				
				lst = this.sqlSessionTemplate.selectList(
						"operater.getRoundDetailsY", productOrder);
			}
		}

		return lst;
	}

	@Override
	public List getroundList(ProductOrder productOrder) {

		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

		if (productOrder.getFlag().equals("<>")) {

			if(productOrder.getRemarks().equals("开料") || productOrder.getRemarks().equals("制帮")){
				
				if(productOrder.getRemarks().equals("开料")){
					
					productOrder.setKzType(1);
					
					lst = this.sqlSessionTemplate.selectList("operater.getRoundListNKType",
							productOrder);
					
				}else if(productOrder.getRemarks().equals("制帮")){
					
					lst = this.sqlSessionTemplate.selectList("operater.getRoundListNZType",
							productOrder);
				}
				
				
			}else {
				
				lst = this.sqlSessionTemplate.selectList("operater.getRoundListN",
						productOrder);
			}
			

		} else if (productOrder.getFlag().equals("=")) {
			
			if(productOrder.getRemarks().equals("开料") || productOrder.getRemarks().equals("制帮")){
				
				if(productOrder.getRemarks().equals("开料")){
					
					productOrder.setKzType(1);
					
					lst = this.sqlSessionTemplate.selectList("operater.getRoundListYKType",
							productOrder);
					
				}else if(productOrder.getRemarks().equals("制帮")){
					
					lst = this.sqlSessionTemplate.selectList("operater.getRoundListYZType",
							productOrder);
				}
				
				
			}else {
				
				lst = this.sqlSessionTemplate.selectList("operater.getRoundListY",
						productOrder);
			}
		}
		
		if(lst != null && lst.size() > 0){
			for(Map<String,Object> map : lst){
				
				ProductOrder order = new ProductOrder();
				
				order.setId(Integer.parseInt(map.get("id").toString()));
				
				List<Map<String, Object>> scrapLst = new ArrayList<Map<String, Object>>();
				
				scrapLst = this.sqlSessionTemplate.selectList("operater.getFedAnalysisByProductOrder",
						order);
				
				int scrapState = 0;
				
				if(scrapLst != null && scrapLst.size() > 0){
					
					scrapState = 1;
				}
				
				map.put("processName", productOrder.getProcessName());
				
				map.put("scrapState", scrapState);
			}
		}
		
		return lst;
	}
	@Override
	public List getRoundListToSeeDetails(ProductOrder productOrder) {
		
		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
		
		lst = this.sqlSessionTemplate.selectList("operater.getRoundListToSeeDetails",
					productOrder);
			
		return lst;
	}
	@Override
	public List getRoundListToSee(ProductOrder productOrder) {
		
		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
		
		lst = this.sqlSessionTemplate.selectList("operater.getRoundListToSee",
				productOrder);
		
		return lst;
	}

	@Override
	public List getpicklists(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList("operater.getpicklists",
				productOrder);
	}
	
	@Override
	public List getFedAnalysis(ProductOrder productOrder) {
		
		return this.sqlSessionTemplate.selectList("operater.getFedAnalysis",
				productOrder);
	}
	
	@Override
	public void updateFedAnalysis(Integer id,Integer feed,Integer checkProcess) {
		
		Map map = new HashMap();
		
		map.put("id", id);
		map.put("feed", feed);
		
		this.sqlSessionTemplate.update("operater.updateFedAnalysis",map);
		
		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("operater.getProductBydefectId",map);
		
		if(lst != null && lst.size() > 0){
			
			int productId = Integer.parseInt(lst.get(0).get("productId").toString());
			
			map.put("productId", productId);
			
			map.put("checkProcess", checkProcess);
			
			List<Map<String,Object>> sLst = this.sqlSessionTemplate.selectList("operater.getScrapStateFeed",map);
			
			if(sLst == null ||  sLst.size() == 0){
				
				map.put("state", 6);
				
				this.sqlSessionTemplate.update("operater.updateState",map);
				
			}
		}
	}

	@Override
	public List getdataList(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList("operater.getdataList",
				productOrder);
	}

	@Override
	public List getCheckSee(ProductOrder productOrder) {
		
		List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
		
		if(productOrder.getRemarks().equals("O")){
			
			lst = this.sqlSessionTemplate.selectList("operater.getCheckSee",
					productOrder);
		}else{
			
			lst = this.sqlSessionTemplate.selectList("operater.getCheckSeeByUserId",
					productOrder);
		}
		
		return lst;
	}
	@Override
	public List getCheckSeeDefectToPicVdo(ProductOrder productOrder) {
		
		List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
		
		if((productOrder.getProcessName().equals("前段") 
				|| productOrder.getProcessName().equals("中段") 
				|| productOrder.getProcessName().equals("后段")) 
				&& productOrder.getRemarks().equals("O")){
			
			lst = this.sqlSessionTemplate.selectList("operater.getCheckSeeDefectToPicVdoByProductId",
					productOrder);
			
		}else {
			
			lst = this.sqlSessionTemplate.selectList("operater.getCheckSeeDefectToPicVdo",
					productOrder);
		}
		
		return lst;
	}

	@Override
	public List getCheckSeeDefect(ProductOrder productOrder) {
		
		List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
		
		
		if(productOrder.getRemarks().equals("T")){
			
			if(productOrder.getProcessName().equals("前段") 
					|| productOrder.getProcessName().equals("中段") 
					||productOrder.getProcessName().equals("后段")){
				
				lst = this.sqlSessionTemplate.selectList("operater.getCheckSeeDefectByProcessId",productOrder);
				
			}else{
				
				lst = this.sqlSessionTemplate.selectList("operater.getCheckSeeDefect",productOrder);
			}
			
		}else{
			
			lst = this.sqlSessionTemplate.selectList("operater.getCheckSeeDefect",productOrder);
		}
		
		return lst;
	}

	@Override
	public List getallfinishroundDetails(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"operater.getallfinishroundDetails", productOrder);
	}

	@Override
	public List getallfinishpicklists(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"operater.getallfinishpicklists", productOrder);
	}

	@Override
	public List getallfinishdataList(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"operater.getallfinishdataList", productOrder);
	}

	@Override
	public List getallfinishroundList(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"operater.getallfinishroundList", productOrder);
	}

	@Override
	public List getallfinishDetails(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"operater.getallfinishDetails", productOrder);
	}
	@Override
	public List<Map<String,Object>> getProcessNameByProductTeam(ProductOrder productOrder) {
		
		return this.sqlSessionTemplate.selectList(
				"planner.getProcessNameByProductTeam", productOrder);
	}

	@Override
	public List getNofinishwork() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getProductTeamsByProductTeamId(ProductOrder productOrder) {
		return this.sqlSessionTemplate.selectList(
				"operater.getProductTeamsByProductTeamId", productOrder);
	}
	@Override
	public List getroundDetails(ProductOrder productOrder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getallfinishList(ProductOrder productOrder) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<Map<String, Object>> checkedAnalysis(ProductOrder productOrder) {
		
		List<Map<String, Object>> lst = this.sqlSessionTemplate.selectList("operater.checkedAnalysisDefect", productOrder);
		
		List<Map<String, Object>> productIdlst = this.sqlSessionTemplate.selectList("operater.checkedAnalysisDefectForProductId", productOrder);
		
		int totalCount = 0;
		
		if(productIdlst != null && productIdlst.size() > 0){
			
			for(Map<String, Object> productIdMap : productIdlst){
				
				productOrder.setId(Integer.parseInt(productIdMap.get("productId").toString()));
				
				//List<Map<String, Object>> productCodeLst = this.sqlSessionTemplate.selectList("operater.checkedAnalysisForProductCode", productOrder);
				
			//	for(Map<String, Object> productCodeMap : productCodeLst){
					
					productOrder.setProductCode(productIdMap.get("productCode").toString());
					
					productOrder.setScheduleNo(productIdMap.get("scheduleNo").toString());
					
					List<Map<String, Object>> totalLst = this.sqlSessionTemplate.selectList("operater.checkedAnalysisTotal", productOrder);
					
					if(totalLst != null && totalLst.size() > 0){
						
						for(Map<String, Object> totalMap : totalLst){
							
							totalCount = totalCount + Integer.parseInt(totalMap.get("productCount").toString());
						}
					}
			//	}
			}
		}
		
		int fgCount = 0;
		
		if(lst != null && lst.size()>0){
			
			for(Map<String, Object> map:lst){
				
				fgCount = fgCount + Integer.parseInt(map.get("defectCount").toString());
			}
		}
		
		for(Map<String, Object> lMap : lst){
			
			lMap.put("totalCount", totalCount);
			lMap.put("fgCount", fgCount);
		}
		
		return lst;
	}

	@Override
	public void insertCheck(Check check) {
		
		this.sqlSessionTemplate.insert("inspector.insertCheck", check);
		
	}
	
	@Override
	public void deleteCheck(Check check) {
		
		this.sqlSessionTemplate.insert("inspector.deleteCheck", check);
		
	}
	
	@Override
	public List getCallTypeUser(Call call) {
		
		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("operater.getCallTypeUser", call);
		
		List<Map<String,Object>> list = this.sqlSessionTemplate.selectList("operater.getEventType");
		
		for(Map<String, Object> CMap:list){
			
			CMap.put("state", 0);
		}
		
		for(Map<String, Object> CMap:list){
				
			for(Map<String, Object> CUMap:lst){
				
				if(CUMap.get("id").toString().equals(CMap.get("id").toString())){
					
					CMap.put("state", 1);//state为0时则显示选择按钮1则显示取消按钮
				}
			}
		}
		
		return list;
		
	}
	@Override
	public List getCallTypeByUser(Call call) {
		
		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("operater.getCallTypeUser", call);
		
		return lst;
		
	}
	@Override
	public List insertCallTypeUser(Call call) {
		
		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("operater.getCallTypeUserById", call);

		if(lst == null || lst.size() == 0){
			
			this.sqlSessionTemplate.insert("operater.insertCallTypeUser", call);
		}
		
		return this.getCallTypeUser(call);
	}
	@Override
	public List getProductTeamsByLevel(int teamLevel) {
		
		Map map = new HashMap();
		map.put("teamLevel", teamLevel);
		
		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("operater.getProductTeamsByLevel", map);
		
		return lst;
	}
	
	@Override
	public List deleteCallTypeUser(Call call) {
		
		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("operater.getCallTypeUserById", call);

		if(lst != null && lst.size() > 0){
			
			this.sqlSessionTemplate.delete("operater.deleteCallTypeUser", call);
		}
		
		return this.getCallTypeUser(call);
		
	}
	@Override
	public List getReferenceMaterial(String finishedProductName) {
		
		Map map = new HashMap();
		
		map.put("finishedProductName", finishedProductName);
		
		List list = new ArrayList();
		
		Map retMap = new HashMap();
		//获取成品信息
		List<Map<String,Object>> fpiLst = this.sqlSessionTemplate.selectList("operater.getFinishedProductInfo", map);
		
		if(fpiLst != null && fpiLst.size() > 0){
			
			String finishedProductCode = fpiLst.get(0).get("finishedProductCode").toString();
			
			map.put("finishedProductCode", finishedProductCode);
			
			//获取参考资料信息
			List<Map<String,Object>> mateLst = this.sqlSessionTemplate.selectList("operater.getReferenceMaterial", map);
			
			//获取参考图片信息
			List<Map<String,Object>> pictLst = this.sqlSessionTemplate.selectList("operater.getReferencePicture", map);
			
			retMap.put("fpiLst", fpiLst);
			
			retMap.put("mateLst", mateLst);
			
			retMap.put("pictLst", pictLst);
		}
		
		list.add(retMap);
		
		return list;
		
	}

}
